package com.amtf.datafile3.core.xsv.render;

import com.amtf.datafile3.DataField;
import com.amtf.datafile3.DataRecord;
import com.amtf.datafile3.DataRecordRender;
import com.amtf.datafile3.core.std.AbsDataRecordRender;

public class XsvDataRecordRender extends AbsDataRecordRender {
	private char delimiter = ',';

	public XsvDataRecordRender(char delimiter) {
		this.delimiter = delimiter;
	}

	public String rending(DataRecord value) {
		DataField[] fields = value.getFields();
		StringBuffer content = new StringBuffer();

		if (fields.length > 0) {
			int max = fields.length - 1;
			for (int i = 0; i < max; i++) {
				if (fields[i] != null) {
					content.append(fields[i].toString());
				} else {
					content.append("");
				}
				content.append(delimiter);
			}

			if (fields[max] != null) {
				content.append(fields[max].toString());
			} else {
				content.append("");
			}
		}

		return content.toString();
	}

	public String rendingTitle(String[] title) {
		StringBuffer content = new StringBuffer();

		int max = title.length - 1;
		for (int i = 0; i < max; i++) {
			if (title[i] != null) {
				content.append(title[i]);
			} else {
				content.append("");
			}
			content.append(delimiter);
		}

		if (title[max] != null) {
			content.append(title[max]);
		} else {
			content.append("");
		}

		return content.toString();
	}

	@Override
	public DataRecordRender clone() {
		XsvDataRecordRender newObj = new XsvDataRecordRender(delimiter);
		return newObj;
	}

	public char getDelimiter() {
		return delimiter;
	}

	public void setDelimiter(char delimiter) {
		this.delimiter = delimiter;
	}
	
}
